********************************************************************************************************
***** Multilevel regressions: Adding sectoral import growth at the employment level (Appendix C.10)*****
********************************************************************************************************
clear
use "BHPS_merged.dta"

do "ML programs to add model statistics.do"			



************************ Merge sectoral import shock: match with d=X based on occupation in t-X *************

*** create industry affiliation from jbsic92 to merge with industry level data 
	capture drop jbsic92_formergeCH
	clonevar jbsic92_formergeCH=jbsic92
	replace jbsic92_formergeCH=1 if (jbsic92>=100 & jbsic92<=150) |  (jbsic92>=200 & jbsic92<=202)  // AGRICULTURE, HUNTING AND FORESTRY
	replace jbsic92_formergeCH=2 if jbsic92>=500 & jbsic92<=502									    // FISHING
	replace jbsic92_formergeCH=3 if jbsic92>=1000 & jbsic92<=1450                                   // MINING AND QUARRYING
	replace jbsic92_formergeCH=4 if jbsic92>=1500 & jbsic92<=1600                                   // Manufacture of Food Products and Beverages and Tobacco
	replace jbsic92_formergeCH=5 if jbsic92>=1700 & jbsic92<=1930                                   // Manufacture of Textiles, Wearing Apparel; Dressing and Dyeing of Fur, Tanning and Dressing of Leather; Manufacture of Handbags, Saddlery, Harness And Footwear
	replace jbsic92_formergeCH=6 if jbsic92>=2000 & jbsic92<=2052                                   // Manufacture of Wood And Products of Wood And Cork, Except Furniture; Manufacture of Articles of Straw & Plaiting Materials
	replace jbsic92_formergeCH=7 if jbsic92>=2100 & jbsic92<=2233                                   // Manufacture of Pulp, Paper & Paper Products, Publishing, Printing & Reproduction of Recorded Media
	replace jbsic92_formergeCH=8 if jbsic92>=2300 & jbsic92<=2330                                   // Manufacture of Coke, Refined Petroleum Products & Nuclear Fuel
	replace jbsic92_formergeCH=9 if jbsic92>=2400 & jbsic92<=2470                                   // Manufacture of basic chemicals, Manufacture of pesticides and other agro-chemical products, Manufacture of paint, varnish & similar coatings, printing inks & cs,Manufacture of soap and detergents, cleaning and polishing preparations, perfumes and toilet preparations, Manufacture of other chemical products, Manufacture of pharmaceuticals, medicinal chemicals, botanical products
	replace jbsic92_formergeCH=10 if jbsic92>=2500 & jbsic92<=2524                                  // Manufacture of Rubber and Plastic Products
	replace jbsic92_formergeCH=11 if jbsic92>=2600 & jbsic92<=2682                                  // Manufacture of Other Non-metallic Mineral Products
	replace jbsic92_formergeCH=12 if jbsic92>=2700 & jbsic92<=2754                                  // Manufacture of Basic Metals
	replace jbsic92_formergeCH=13 if jbsic92>=2800 & jbsic92<=2875                                  // Manufacture of fabricated metal products, except machinery & equipment
	replace jbsic92_formergeCH=14 if jbsic92>=2900 & jbsic92<=2972                                  // Manufacture of Machinery and Equipment Not Elsewhere Classified
	replace jbsic92_formergeCH=15 if jbsic92>=3000 & jbsic92<=3002                                  // Manufacture of office machinery and computers
	replace jbsic92_formergeCH=16 if jbsic92>=3100 & jbsic92<=3162                                  // Manufacture of Electrical Machinery & Apparatus Not Elsewhere Classified
	replace jbsic92_formergeCH=17 if jbsic92>=3200 & jbsic92<=3230                                  // Manufacture of Radio, Television, Communication Equipment & Apparatus
	replace jbsic92_formergeCH=18 if jbsic92>=3300 & jbsic92<=3350                                  // Manufacture of Medical, Precision and Optical Instruments, Watches and Clocks
	replace jbsic92_formergeCH=19 if jbsic92>=3400 & jbsic92<=3430                                  // Manufacture of Motor Vehicles, Trailers and Semi-trailers
	replace jbsic92_formergeCH=20 if jbsic92>=3500 & jbsic92<=3550                                  // Manufacture of Other Transport Equipment
	replace jbsic92_formergeCH=21 if jbsic92>=3600 & jbsic92<=3720                                  // Manufacture of Furniture; Manufacturing Not Elsewhere Classified

	replace jbsic92_formergeCH=99 if jbsic92>=4000 & jbsic92<=9999                                  // Other activities
	replace jbsic92_formergeCH=99 if jbsic92==-9 | jbsic92==-8 | jbsic92==-7                        // Sector missing


	replace jbsic92_formergeCH=. if jbsic92==410 | jbsic92==802 | jbsic92==810 | jbsic92==853 | jbsic92==2560 | jbsic92==3021 | jbsic92==3284   /// weird codes that shouldn't exist -> to missing



*** merge sectoral import data 
	forvalues i=1/10 	{ 
			gen jbsic92_formergeCH_l`i'=L`i'.jbsic92_formergeCH
					}

	forvalues i=1/10 	{ 
			merge m:1 jbsic92_formergeCH_l`i' year using "Sector_imports.dta", keepusing(sector_real_imp_CH_gr_d`i' sector_imprat_CH_gr_d`i' sect_imp_incrperw_CH_d`i') nogenerate
					}
					
		
******** Regressions ********	
	global controls "i.education i.male c.age_cent##c.age_cent i.bornelsewhere2 i.parentsbornelsewhere"
	sort pid waven
	
*** only sectors concerned

	* take neglog of sectoral growth rate
		gen sector_real_imp_CH_gr_d3_nl = sign(sector_real_imp_CH_gr_d3) * ln(abs(sector_real_imp_CH_gr_d3)+1)
		gen sector_real_imp_CH_gr_d4_nl = sign(sector_real_imp_CH_gr_d4) * ln(abs(sector_real_imp_CH_gr_d4)+1)
	
	* Regressions (2002 observations missing)
		
	* EU support
	mixed D_EUsupport c.LDV_EUsupport ///
					real_gr_imp_CH_d4_nl sector_real_imp_CH_gr_d4_nl ///
					$controls i.ID_NUTS1xYear ///
					if (year==2006) ///
					|| ID_NUTS3xYear: , stddeviations
					
		add_scalars_mixed
		eststo EU1
		sum D_EUsupport real_gr_imp_CH_d4_nl sector_real_imp_CH_gr_d4_nl if e(sample)
	
	* Nationalism
	mixed D3_nationalism L3.nationalism  ///
					real_gr_imp_CH_d3_nl sector_real_imp_CH_gr_d3_nl ///
					$controls i.ID_NUTS1xYear ///
					if (year==2008 | year==2005) ///
					|| ID_NUTS3: || ID_NUTS3xYear: , stddeviations 
					
		add_scalars_mixed_2level
		eststo NAT1
		sum D3_nationalism real_gr_imp_CH_d3_nl sector_real_imp_CH_gr_d3_nl if e(sample)

*** all respondents
 * set import growth rates zero for all "others" (not [known to be] employed in one of the sectors for which we have data in t-X)
	clonevar sector_real_imp_CH_gr_d3_B=sector_real_imp_CH_gr_d3
	clonevar sector_real_imp_CH_gr_d4_B=sector_real_imp_CH_gr_d4

	replace sector_real_imp_CH_gr_d3_B=0 if (jbsic92_formergeCH_l3<1 | jbsic92_formergeCH_l3==99) & ( (year==2005) | (year==2008) )
	replace sector_real_imp_CH_gr_d4_B=0 if (jbsic92_formergeCH_l4<1 | jbsic92_formergeCH_l4==99) & (year==2006)
			
	* take neglog of sectoral growth rate
	gen sector_real_imp_CH_gr_d3_B_nl = sign(sector_real_imp_CH_gr_d3_B) * ln(abs(sector_real_imp_CH_gr_d3_B)+1)
	gen sector_real_imp_CH_gr_d4_B_nl = sign(sector_real_imp_CH_gr_d4_B) * ln(abs(sector_real_imp_CH_gr_d4_B)+1)		
	
	* Regressions (2002 observations missing)
	* EU support
	mixed D_EUsupport c.LDV_EUsupport ///
					real_gr_imp_CH_d4_nl sector_real_imp_CH_gr_d4_B_nl ///
					$controls i.ID_NUTS1xYear ///
					if (year==2006) ///
					|| ID_NUTS3xYear: , stddeviations reml
					
		add_scalars_mixed
		eststo EU2
		sum D_EUsupport real_gr_imp_CH_d4_nl sector_real_imp_CH_gr_d4_B_nl if e(sample)

	* Nationalism
		mixed D3_nationalism L3.nationalism  ///
						real_gr_imp_CH_d3_nl sector_real_imp_CH_gr_d3_B_nl ///
						$controls i.ID_NUTS1xYear ///
						if (year==2008 | year==2005) ///
						|| ID_NUTS3: || ID_NUTS3xYear: , stddeviations 
						
			add_scalars_mixed_2level
			eststo NAT2
			sum D3_nationalism real_gr_imp_CH_d3_nl sector_real_imp_CH_gr_d3_B_nl if e(sample)		



************************ Merge sectoral import shock: match based on occupation in t *************
	clear
	use "BHPS_merged.dta"

*** create industry affiliation from jbsic92 to merge with industry level data 
	capture drop jbsic92_formergeCH
	clonevar jbsic92_formergeCH=jbsic92
	replace jbsic92_formergeCH=1 if (jbsic92>=100 & jbsic92<=150) |  (jbsic92>=200 & jbsic92<=202)  // AGRICULTURE, HUNTING AND FORESTRY
	replace jbsic92_formergeCH=2 if jbsic92>=500 & jbsic92<=502									    // FISHING
	replace jbsic92_formergeCH=3 if jbsic92>=1000 & jbsic92<=1450                                   // MINING AND QUARRYING
	replace jbsic92_formergeCH=4 if jbsic92>=1500 & jbsic92<=1600                                   // Manufacture of Food Products and Beverages and Tobacco
	replace jbsic92_formergeCH=5 if jbsic92>=1700 & jbsic92<=1930                                   // Manufacture of Textiles, Wearing Apparel; Dressing and Dyeing of Fur, Tanning and Dressing of Leather; Manufacture of Handbags, Saddlery, Harness And Footwear
	replace jbsic92_formergeCH=6 if jbsic92>=2000 & jbsic92<=2052                                   // Manufacture of Wood And Products of Wood And Cork, Except Furniture; Manufacture of Articles of Straw & Plaiting Materials
	replace jbsic92_formergeCH=7 if jbsic92>=2100 & jbsic92<=2233                                   // Manufacture of Pulp, Paper & Paper Products, Publishing, Printing & Reproduction of Recorded Media
	replace jbsic92_formergeCH=8 if jbsic92>=2300 & jbsic92<=2330                                   // Manufacture of Coke, Refined Petroleum Products & Nuclear Fuel
	replace jbsic92_formergeCH=9 if jbsic92>=2400 & jbsic92<=2470                                   // Manufacture of basic chemicals, Manufacture of pesticides and other agro-chemical products, Manufacture of paint, varnish & similar coatings, printing inks & cs,Manufacture of soap and detergents, cleaning and polishing preparations, perfumes and toilet preparations, Manufacture of other chemical products, Manufacture of pharmaceuticals, medicinal chemicals, botanical products
	replace jbsic92_formergeCH=10 if jbsic92>=2500 & jbsic92<=2524                                  // Manufacture of Rubber and Plastic Products
	replace jbsic92_formergeCH=11 if jbsic92>=2600 & jbsic92<=2682                                  // Manufacture of Other Non-metallic Mineral Products
	replace jbsic92_formergeCH=12 if jbsic92>=2700 & jbsic92<=2754                                  // Manufacture of Basic Metals
	replace jbsic92_formergeCH=13 if jbsic92>=2800 & jbsic92<=2875                                  // Manufacture of fabricated metal products, except machinery & equipment
	replace jbsic92_formergeCH=14 if jbsic92>=2900 & jbsic92<=2972                                  // Manufacture of Machinery and Equipment Not Elsewhere Classified
	replace jbsic92_formergeCH=15 if jbsic92>=3000 & jbsic92<=3002                                  // Manufacture of office machinery and computers
	replace jbsic92_formergeCH=16 if jbsic92>=3100 & jbsic92<=3162                                  // Manufacture of Electrical Machinery & Apparatus Not Elsewhere Classified
	replace jbsic92_formergeCH=17 if jbsic92>=3200 & jbsic92<=3230                                  // Manufacture of Radio, Television, Communication Equipment & Apparatus
	replace jbsic92_formergeCH=18 if jbsic92>=3300 & jbsic92<=3350                                  // Manufacture of Medical, Precision and Optical Instruments, Watches and Clocks
	replace jbsic92_formergeCH=19 if jbsic92>=3400 & jbsic92<=3430                                  // Manufacture of Motor Vehicles, Trailers and Semi-trailers
	replace jbsic92_formergeCH=20 if jbsic92>=3500 & jbsic92<=3550                                  // Manufacture of Other Transport Equipment
	replace jbsic92_formergeCH=21 if jbsic92>=3600 & jbsic92<=3720                                  // Manufacture of Furniture; Manufacturing Not Elsewhere Classified

	replace jbsic92_formergeCH=99 if jbsic92>=4000 & jbsic92<=9999                                  // Other activities
	replace jbsic92_formergeCH=99 if jbsic92==-9 | jbsic92==-8 | jbsic92==-7                        // Sector missing


	replace jbsic92_formergeCH=. if jbsic92==410 | jbsic92==802 | jbsic92==810 | jbsic92==853 | jbsic92==2560 | jbsic92==3021 | jbsic92==3284   /// weird codes that shouldn't exist -> to missing


	tab jbsic92_formergeCH


*** merge sectoral import data 
	forvalues i=1/10 	{ 
			merge m:1 jbsic92_formergeCH year using "Sector_imports.dta", keepusing(sector_real_imp_CH_gr_d`i' 	sector_imprat_CH_gr_d`i' sect_imp_incrperw_CH_d`i') nogenerate
					}
			
******** Regressions ********	
	sort pid waven

*** only sectors concerned

	* take neglog of sectoral growth rate
		gen sector_real_imp_CH_gr_d3_nl = sign(sector_real_imp_CH_gr_d3) * ln(abs(sector_real_imp_CH_gr_d3)+1)
		gen sector_real_imp_CH_gr_d4_nl = sign(sector_real_imp_CH_gr_d4) * ln(abs(sector_real_imp_CH_gr_d4)+1)
	
	* Regressions (2002 observations missing)
		global controls "i.education i.male c.age_cent##c.age_cent i.bornelsewhere2 i.parentsbornelsewhere"
		
		sort pid waven
		
	* EU support
		mixed D_EUsupport c.LDV_EUsupport##i.year ///
						real_gr_imp_CH_d4_nl sector_real_imp_CH_gr_d4_nl ///
						$controls i.ID_NUTS1xYear ///
						if (year==2006 | year==2002) ///
						|| ID_NUTS3: || ID_NUTS3xYear: , stddeviations 
			
			add_scalars_mixed_2level
			eststo EU3
			sum D_EUsupport real_gr_imp_CH_d4_nl sector_real_imp_CH_gr_d4_nl if e(sample)
	
	* Nationalism
		mixed D3_nationalism L3.nationalism  ///
						real_gr_imp_CH_d3_nl sector_real_imp_CH_gr_d3_nl ///
						$controls i.ID_NUTS1xYear ///
						if (year==2008 | year==2005 | year==2002) ///
						|| ID_NUTS3: || ID_NUTS3xYear: , stddeviations 
			
			add_scalars_mixed_2level
			eststo NAT3
			sum D3_nationalism real_gr_imp_CH_d3_nl sector_real_imp_CH_gr_d3_nl if e(sample)

		
*** all respondents
 * set import growth rates zero for all "others" (not [known to be] employed in one of the sectors for which we have data in t-X)
		clonevar sector_real_imp_CH_gr_d3_B=sector_real_imp_CH_gr_d3
		clonevar sector_real_imp_CH_gr_d4_B=sector_real_imp_CH_gr_d4

		replace sector_real_imp_CH_gr_d3_B=0 if (jbsic92_formergeCH<1 | jbsic92_formergeCH==99) & ( (year==2002) | (year==2005) | (year==2008) )
		replace sector_real_imp_CH_gr_d4_B=0 if (jbsic92_formergeCH<1 | jbsic92_formergeCH==99) & ( (year==2002) | (year==2006) )
		
		* take neglog of sectoral growth rate
		gen sector_real_imp_CH_gr_d3_B_nl = sign(sector_real_imp_CH_gr_d3_B) * ln(abs(sector_real_imp_CH_gr_d3_B)+1)
		gen sector_real_imp_CH_gr_d4_B_nl = sign(sector_real_imp_CH_gr_d4_B) * ln(abs(sector_real_imp_CH_gr_d4_B)+1)		
		
		* Regressions (2002 observations missing)

		* EU support
		mixed D_EUsupport c.LDV_EUsupport##i.year ///
						real_gr_imp_CH_d4_nl sector_real_imp_CH_gr_d4_B_nl ///
						$controls i.ID_NUTS1xYear ///
						if (year==2006 | year==2002) ///
						|| ID_NUTS3: || ID_NUTS3xYear: , stddeviations reml
			
			add_scalars_mixed_2level
			eststo EU4
			sum D_EUsupport real_gr_imp_CH_d4_nl sector_real_imp_CH_gr_d4_B_nl if e(sample)
		
		* Nationalism
		mixed D3_nationalism L3.nationalism  ///
						real_gr_imp_CH_d3_nl sector_real_imp_CH_gr_d3_B_nl ///
						$controls i.ID_NUTS1xYear ///
						if (year==2008 | year==2005 | year==2002) ///
						|| ID_NUTS3: || ID_NUTS3xYear: , stddeviations 
			
			add_scalars_mixed_2level
			eststo NAT4
			sum D3_nationalism real_gr_imp_CH_d3_nl sector_real_imp_CH_gr_d3_B_nl if e(sample)
	
	
	
************************ Write regression tables *************
* EU support
	esttab EU1 EU2 EU3 EU4 using "ML regressions with sectoral_EUsupport.rtf", b(a2) se(a2) star(+ 0.10 * 0.05 ** 0.01 *** 0.001) ///
		scalars(group1N group2N group3N ri1_std ri2_std ri3_std icc1 icc2 icc3 bic) varwidth(30) nogaps compress ///
		drop(0.male 0.education 0.bornelsewhere2 0.parentsbornelsewhere *.ID_NUTS1xYear lnsig_e:_cons) label replace /// 
		transform(ln*: exp(@) exp(@)) 						
				
* Nationalism
	esttab NAT1 NAT2 NAT3 NAT4 using "ML regressions with sectoral_Nationalism.rtf", b(a2) se(a2) star(+ 0.10 * 0.05 ** 0.01 *** 0.001) ///
		scalars(group1N group2N group3N ri1_std ri2_std ri3_std icc1 icc2 icc3 bic) varwidth(30) nogaps compress ///
		drop(0.male 0.education 0.bornelsewhere2 0.parentsbornelsewhere *.ID_NUTS1xYear lnsig_e:_cons) label replace /// 
		transform(ln*: exp(@) exp(@)) 			